package com.fyzk.activity.lottery.basketball.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseFragment;
import com.fyzk.activity.lottery.basketball.JCLQFragmentActivityI;
import com.fyzk.activity.user.NetProgressView;
import com.fyzk.adapter.JCLQBaseAdapter;
import com.fyzk.bean.JCLQResponseInfo;
import com.fyzk.interfaces.ReTryInterface;
import com.tyjc.lottery.R;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.net.Commands;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * 竞彩篮球的基类Fragment
 *
 * @author miao
 */
public abstract class BasketballWFBaseFragment extends BaseFragment implements
        ReTryInterface {

    protected View mainView;

    public int lotteryId = 0;

    public boolean isDanguan;

    private NetProgressView netProgress;
    protected ExpandableListView exListView;

    public ArrayList<String> teams = new ArrayList<>();
    public ArrayList<String> teams_sel = new ArrayList<>();

    private boolean isActivityResult = false;// icon_jieshao_title

    public List<JCLQResponseInfo> infos = new ArrayList<>();

    public List<JCLQResponseInfo> infos_all = new ArrayList<>();
    protected JCLQBaseAdapter adapter;

    JCLQFragmentActivityI activity;

    public void notifyList() {
        if (infos.size() == 0) {
            net();
        } else {
            adapterChange();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub'mainVs
        activity = (JCLQFragmentActivityI) getActivity();
        lotteryId = getLotteryId();
        mainView = inflater.inflate(R.layout.layout_jclq_selectgame, null);
        netProgress = mainView.findViewById(R.id.jclq_vp_netprogressview);
        exListView = mainView.findViewById(R.id.jclq_center_list);
        exListView.setGroupIndicator(null);
        netProgress.setRetryInterface(this);
        return mainView;
    }


    protected abstract int getLotteryId();

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    private void net() {
        netProgress.waiting();
        findJclqRace();
    }


    private void findJclqRace() {
        JSONObject obj = new JSONObject();
        try {
            obj.put("lotteryType", getLotteryId());
            if (isDanguan) {
                obj.put("type", "01");
            } else {
                obj.put("type", "02");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        httpPostJson(CommandUtil.JCLQGAME_QUEUE, Commands.FINDJCLQRACE, obj, false);
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        netProgress.dismiss();
        retryCount = 0;
        try {
            JSONObject obj = new JSONObject(resultBean.getData());
            Iterator<String> iterator = obj.keys();
            while (iterator.hasNext()) {
                List<JCLQMatchData> matchDatas = new ArrayList<>();
                JCLQResponseInfo info = new JCLQResponseInfo();
                String phase = iterator.next();
                String matchsStr = obj.getString(phase);
                JSONArray jsonArray = new JSONArray(matchsStr);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject string = jsonArray.getJSONObject(i);
                    JCLQMatchData matchData = JSON.parseObject(string.toString(), JCLQMatchData.class);
                    matchData.sortPlInfos();
                    matchDatas.add(matchData);
                }
                info.matchDatas = matchDatas;
                info.matchTime = phase;
                info.phaseNo = phase;
                info.lotteryType = getLotteryId();
                info.matchWeekday = matchDatas.get(0).getOfficialWeekDay();
                infos_all.add(info);
            }
            Collections.sort(infos_all);
            infos.addAll(infos_all);
            adapterChange();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //错误重试次数
    int retryCount = 0;

    @Override
    public void onFailure(int requestCode, String errorcode, String msg) {
        //报系统错误，重试3次
        if ("9999".equals(errorcode) && retryCount < 3) {
            retryCount++;
            reTry();
        } else {
            retryCount = 0;
            netProgress.noneRecord(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // TODO Auto-generated method stub
        isActivityResult = true;
    }

    protected void adapterChange() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void clear() {
        clearSel();
        adapterChange();
    }

    public void clearSel() {
        for (int i = 0; i < infos_all.size(); i++) {
            if (infos_all.size() > 0) {
                for (int j = 0; j < infos_all.get(i).matchDatas.size(); j++) {
                    JCLQMatchData data = infos_all.get(i).matchDatas.get(j);
                    data.clickNum = 0;
                    data.setSfcNum(0);
                    for (int k = 0; k < data.plInfos.size(); k++) {
                        data.plInfos.get(k).isSel = false;
                    }
                }
            }
        }
    }


    public int countChangCi() {
        int changCi = 0;
        for (int i = 0; i < infos_all.size(); i++) {
            for (int j = 0; j < infos_all.get(i).matchDatas.size(); j++) {
                JCLQMatchData data = infos_all.get(i).matchDatas.get(j);
                if (data.clickNum > 0) {
                    changCi++;
                }
            }
        }
        return changCi;
    }

    @Override
    public void reTry() {
        // TODO Auto-generated method stub
        net();
    }

    /**
     * 筛选赛事后
     */
    public void onFilterOK(ArrayList<String> teams_sel) {
        infos.clear();
        for (int i = 0; i < infos_all.size(); i++) {
            JCLQResponseInfo info_ = infos_all.get(i).copy();
            if (!infos.contains(info_)) {
                infos.add(info_);
            }
            List<JCLQMatchData> datas = infos_all.get(i).matchDatas;
            for (JCLQMatchData data : datas) {
                if (teams_sel.contains(data.getMatchName())) {
                    infos.get(i).matchDatas.add(data);
                }
            }
        }
        adapterChange();
    }

    public abstract String getTextStr();

    public ArrayList<String> getTeams() {
        teams.clear();
        for (int i = 0; i < infos_all.size(); i++) {
            List<JCLQMatchData> datas = infos_all.get(i).matchDatas;
            for (int j = 0; j < datas.size(); j++) {
                if (!teams.contains(datas.get(j).getMatchName())) {
                    teams.add(datas.get(j).getMatchName());
                }
            }
        }
        return teams;
    }

    public ArrayList<String> getTeamsSel() {
        return teams_sel;
    }

}
